#include "headfile.h"
#include "xunji.h"
#include "dmreg.h"
sbit qian_1 = P1^0;
sbit qian_2 = P1^1;
sbit qian_3 = P1^2;
sbit qian_4 = P1^3;

sbit zuo_1 = P0^1;
sbit zuo_2 = P0^4;
sbit zuo_3 = P0^5;
sbit zuo_4 = P0^6;

sbit hou_1 = P3^2;
sbit hou_2 = P3^3;
sbit hou_3 = P3^6;
sbit hou_4 = P3^7;

sbit key_1=P7^0;
sbit key_2=P7^1;


bit cont_Q=0,cont_Z=0,cont_H=0,cont_Y=0;
char count_w = 0,tiao=0;
uint8 zhua[2];
extern uint8 shuju[200];
extern bit biaozhiwei;
void diyiduan()
{
    uart_putbuff(UART_3,&DM_Action100,7);
	car_xz();
	while(1)
	{
		
		count_z(2);
		if(count_w>=1)
		{
			delay_ms(30);
			car_stop();
			count_w=0;
			break;
		}				
	}
	car_xq();
    delay_ms(400);
    count_w=0;
	while(1)
	{
		
		xunji(1);
		count_z(1);
		if(count_w>=3)
		{
			delay_ms(30);
			car_stop();
			count_w=0;
			while(1)
			{
//				if(key_1==0)
					break;
			}
			break;
		}
	}
	car_xz();
    delay_ms(300);
	count_w=0;
	while(1)
	{
		xunji(2);
		count_z(2);
		if(count_w>=4)
		{
			delay_ms(20);
			car_stop();
			count_w=0;
			break;
		}
	
	}
	car_xq();
	delay_ms(300);
	count_w=0;
	while(1)
	{
		xunji(1);
		count_z(1);
		if(count_w>=1)
		{
			delay_ms(20);
			car_stop();
			count_w=0;
            for(tiao=0;tiao<100;tiao++)
            {
                tiaozheng();
                delay_ms(10);
            }
			break;
		}
	
	}
    car_xh();
	delay_ms(300);
	count_w=0;
	while(1)
	{
		xunji(3);
		count_z(3);
		if(count_w>=1)
		{
			delay_ms(20);
			car_stop();
			count_w=0;
			break;
		}
	
	}
    car_xy();
    delay_ms(300);
	count_w=0;
	while(1)
	{
		xunji(4);
		count_z(4);
		if(count_w>=4)
		{
			delay_ms(20);
			car_stop();
			count_w=0;
			break;
		}
	
	}
    car_xh();
    delay_ms(400);
    count_w=0;
	while(1)
	{
		
		xunji(3);
		count_z(3);
		if(count_w>=2&&(qian_1==1&&qian_2==1&&qian_3==1&&qian_4==1))
		{
			delay_ms(30);
			car_stop();
			count_w=0;
			while(1)
			{
//				if(key_1==0)
					break;
			}
			break;
		}
	}
    car_xy();
    count_w=0;
	while(1)
	{
		if(zuo_1==1&&zuo_2==1&&zuo_3==1&&zuo_4==1)
		{
			delay_ms(30);
			car_stop();
			count_w=0;
			while(1)
			{
//				if(key_1==0)
//					break;
			}
			break;
		}
	}
}


void zhuang_1()
{
	
	if(shuju[0]=='1')
	{
		uart_putbuff(UART_3,&DM_Action1,7);
		delay_ms(8000);
        uart_putbuff(UART_3,&DM_Action100,7);
		delay_ms(3000);
        
	}
	else if(shuju[0]=='2')
	{
		uart_putbuff(UART_3,&DM_Action2,7);
		delay_ms(8000);
        uart_putbuff(UART_3,&DM_Action100,7);
		delay_ms(3000);
	}

	
}


void count_z(char xiang)
{

	if(xiang==1)
	{
        if(cont_Q==0&&zuo_3==1)
        {
            delay_ms(10);
            if(cont_Q==0&&zuo_3==1)
            {
                count_w++;
                led_1=!led_1;
                cont_Q=1;
            }
        }
        else if(cont_Q==1&&zuo_3==0)
        {
            delay_ms(200);
            if(cont_Q==1&&zuo_3==0)
            {
                cont_Q=0;					
            }
        }
	}
		else if(xiang==2)
		{
				if(cont_Z==0&&qian_2==1)
				{
					delay_ms(20);
					if(cont_Z==0&&qian_2==1)
					{
						count_w++;
						led_1=!led_1;
						cont_Z=1;
					}
				}
				else if(cont_Z==1&&qian_3==0)
				{
					delay_ms(200);
					if(cont_Z==1&&qian_3==0)
					{
						cont_Z=0;				
					}					
				}
		}
		else if(xiang==3)
		{
				if(cont_H==0&&zuo_2==1)
				{
					delay_ms(20);
					if(cont_H==0&&zuo_2==1)
					{
						count_w++;
						led_1=!led_1;
						cont_H=1;
					}
				}
				else if(cont_H==1&&zuo_2==0)
				{
					delay_ms(200);
					if(cont_H==1&&zuo_2==0)
					{
						cont_H=0;				
					}					
				}
		}

	

}

void ce_xunji(uint8 fang)
{
    if(fang == 1)
    {   
        while(1)
        {
            car_xq();
//            xunji(1);
            count_z(1);
            if(count_w>=4)
            {
                delay_ms(30);
                car_stop();
                count_w=0;
                break;
            }		
        }

    }
    if(fang == 2)
    {   
        while(1)
        {
            car_xz();
//            xunji(2);
            count_z(2);
            if(count_w>=4)
            {
                delay_ms(30);
                car_stop();
                count_w=0;
                break;
            }		
        }

    }
    if(fang == 3)
    {   
        while(1)
        {
            car_xh();
//            xunji(3);
            count_z(3);
            if(count_w>=4)
            {
                delay_ms(30);
                car_stop();
                count_w=0;
                break;
            }		
        }

    }
    if(fang == 4)
    {   
        while(1)
        {
            car_xy();
//            xunji(4);
            count_z(4);
            if(count_w>=4)
            {
                delay_ms(30);
                car_stop();
                count_w=0;
                break;
            }		
        }

    }

}
